package com.pink.controller;

import com.pink.entity.dto.UserDto;
import com.pink.entity.query.VideoFilesQuery;
import com.pink.entity.vo.ResponseVo;
import com.pink.entity.vo.request.VideoFileVo;
import com.pink.entity.vo.response.VideoNameVo;
import com.pink.exception.BusinessException;
import com.pink.service.VideoFileService;
import com.pink.service.VideoInfoService;
import com.pink.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/admin/video")
@Validated
public class VideoFileController extends ABaseController{

    @Autowired
    VideoInfoService videoInfoService;

    @Autowired
    VideoFileService videoFileService;

    /**
     * 加载数据
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/loadFileDataList")
    public ResponseVo<VideoFilesQuery> loadFileDataList(
            // 页码，默认为1
            @RequestParam(value = "PageNo", defaultValue = "1") int pageNo,
            // 每页大小，默认为15
            @RequestParam(value = "PageSize", defaultValue = "15") int pageSize,String title) {
        return ResponseVo.success(videoFileService.findFileQueryList(pageNo, pageSize,title));
    }

    /**
     * 获取视频名称列表
     * @return
     */
    @RequestMapping("/videoNameList")
    public ResponseVo<List<VideoNameVo>> videoNameList() {
        return ResponseVo.success(videoInfoService.findVideoNameList());
    }

    /**
     * 添加视频文件信息
     * @return
     */
    @RequestMapping("/addVideoFile")
    public ResponseVo<String> videoInfoList(@NotNull Integer episodeNumber, @NotNull Integer videoId, @NotNull String videoList) {
        UserDto user = getTokenUserDto();
        if(user == null) {
            throw new BusinessException("未登录");
        }
        List<VideoFileVo> videoFileVo = JsonUtils.convertJsonArray2List(videoList, VideoFileVo.class);
        return ResponseVo.success(videoFileService.addVideoFile(user,episodeNumber, videoId, videoFileVo));
    }
}
